<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link rel="shortcut icon" href="favicon.ico">
    <link href="__PUBLIC__/admin/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__PUBLIC__/admin/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="__PUBLIC__/admin/css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/Huploadify/Huploadify.css"/>
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script src="__PUBLIC__/admin/js/jquery.min.js?v=2.1.4"></script>
    <script src="__PUBLIC__/admin/js/jquery-ui-1.10.4.custom.min.js"></script>
    <style>
        input.error{
            border:1px solid red;
        }
        span.error{
            padding-top:10px;
            color: #f00;
            font-size:12px;
        }
    </style>
</head>

<script>

    {if condition="isset($pnum)"}
        var pnum = {$pnum};
    {else/}
        var pnum = 0;
    {/if}
    {if condition="(!isset($search)) OR (!$search)"}
        var search = 0;
        var goUrl = "/{$Request.module}/dispatch/lst.html?page="+pnum;
    {else/}
        var search = {$search};
        var goUrl = "/{$Request.module}/dispatch/search.html?page="+pnum;
    {/if}
    var postUrl = "{:url('dispatch/post')}";
    var url =  "/{$Request.module}/dispatch";
</script>

<body class="gray-bg">
<div class="wrapper wrapper-content" id="server">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>{if !empty($dispatch['id'])}编辑{else/}添加{/if}配送方式 <small>{if !empty($dispatch['id'])}修改【{$dispatch['dispatch_name']}】{/if}</small></h5>
                    <span class='pull-right'>
                        <button class="btn btn-default  btn-sm" onclick="cl()">返回列表</button>
                    </span>
                </div>
                <div class="ibox-content">
                    <form action="" method="post" id="form" class="form-horizontal form-validate" enctype="multipart/form-data">
                    <input type="hidden" name="id" value="{$dispatch['id']}" />
                    <div class="form-group">
                        <label class="col-sm-2 control-label">排序</label>
                        <div class="col-sm-9 col-xs-12">
                            <input type="text" name="sort" class="form-control" value="{$dispatch['sort']}" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label must">配送方式名称</label>
                        <div class="col-sm-9 col-xs-12">
                            <input type="text" id='dispatch_name' name="dispatchname" class="form-control" value="{$dispatch['dispatch_name']}" data-rule-required='true' />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label ">是否默认</label>

                        <div class="col-sm-9 col-xs-12">

                            <label class='radio-inline'>
                                <input type='radio' name='isdefault' id="isdefault1" value='1' {if $dispatch['is_default']==1}checked{/if} /> 是
                            </label>
                            <label class='radio-inline'>
                                <input type='radio' name='isdefault' id="isdefault0" value='0' {if $dispatch['is_default']==0}checked{/if} /> 否
                            </label>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label ">计费方式</label>

                        <div class="col-sm-9 col-xs-12">

                            <label class='radio-inline'>
                                <input type='radio' name='calculatetype' value='0' {if $dispatch['calculate_type']==0}checked{/if} /> 按重量计费
                            </label>
                            <label class='radio-inline'>
                                <input type='radio' name='calculatetype' value='1' {if $dispatch['calculate_type']==1}checked{/if} /> 按件计费
                            </label>

                        </div>
                    </div>

                    <div class="form-group dispatch0">
                        <label class="col-sm-2 control-label ">物流公司</label>
                        <div class="col-sm-9 col-xs-12">

                            <select name='express' class="form-control select2">
                                <option value="" data-name="其他快递">其他快递</option>

                                {foreach $express_list as $value}
                                <option value="{$value['log_name']}" data-name="{$value['log_name']}">{$value['log_name']}</option>
                                {/foreach}

                            </select>
                            <span class="help-block">如果您选择了常用快递，则客户可以订单中查询快递信息，如果缺少您想要的快递，您可以联系我们! </span>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label ">配送区域</label>
                        <div class="col-sm-9 col-xs-12">

                            <table  style='width:110%;'>
                                <thead>
                                <tr>
                                    <th style="height:40px;width:400px;">运送到</th>
                                    <th class="show_h" style="width:100px;">首重(克)</th>
                                    <th class="show_h" style="width:100px;">首费(元)</th>
                                    <th class="show_h" style="width:100px;">续重(克)</th>
                                    <th class="show_h" style="width:100px;">续费(元)</th>

                                    <th class="show_n" style="width:100px;">首件(个)</th>
                                    <th class="show_n" style="width:100px;">运费(元)</th>
                                    <th class="show_n" style="width:100px;">续件(个)</th>
                                    <th class="show_n" style="width:100px;">续费(元)</th>
                                    <th style="width:100px;">管理</th>
                                </tr>
                                </thead>
                                <tbody id='tbody-areas'>
                                <tr>
                                    <td style="padding:10px;">全国 [默认运费]</td>
                                    <td class="show_h text-center">

                                        <input type="text" value="{php} echo empty($dispatch['first_weight']) ? 1000 : $dispatch['first_weight'] {/php}" class="form-control" name="default_firstweight" style="width:80px;"></td>

                                    <td class="show_h text-center">

                                        <input type="text" value="{$dispatch['first_price']}" class="form-control" name="default_firstprice"  style="width:80px;"></td>

                                    <td class="show_h text-center">

                                        <input type="text" value="{php} echo empty($dispatch['second_weight']) ? 1000 : $dispatch['second_weight'] {/php}" class="form-control" name="default_secondweight"  style="width:80px;">

                                    </td>
                                    <td class="show_h text-center">

                                        <input type="text" value="{$dispatch['second_price']}" class="form-control" name="default_secondprice"  style="width:80px;">

                                    </td>

                                    <td class="show_n text-center">

                                        <input type="text" value="{php} echo empty($dispatch['first_num'])?1:$dispatch['first_num']{/php}" class="form-control" name="default_firstnum" style="width:80px;">

                                    </td>
                                    <td class="show_n text-center">

                                        <input type="text" value="{$dispatch['first_num_price']}" class="form-control" name="default_firstnumprice"  style="width:80px;">

                                    </td>
                                    <td class="show_n text-center">

                                        <input type="text" value="{php} echo empty($dispatch['second_num'])?1:$dispatch['second_num']{/php}" class="form-control" name="default_secondnum"  style="width:80px;">

                                    </td>
                                    <td class="show_n text-center">

                                        <input type="text" value="{$dispatch['second_num_price']}" class="form-control" name="default_secondnumprice"  style="width:80px;">

                                    </td>


                                </tr>
                                {foreach $dispatch_areas as $row}
                                {include file="dispatch/tpl" /}
                                {/foreach}
                                </tbody>
                            </table>

                            <a class='btn btn-default' href="javascript:;" onclick='addArea(this)'><span class="fa fa-plus"></span> 新增配送区域</a>
                            <span class='help-block show_h' {if $dispatch['calculate_type']==1}style='display:none'{/if}>根据重量来计算运费，当物品不足《首重重量》时，按照《首重费用》计算，超过部分按照《续重重量》和《续重费用》乘积来计算</span>
                            <span class='help-block show_n' {if $dispatch['calculate_type']==0}style='display:none'{/if}>根据件数来计算运费，当物品不足《首件数量》时，按照《首件费用》计算，超过部分按照《续件重量》和《续件费用》乘积来计算</span>



                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">特殊区域设置</label>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label ">类型</label>

                        <div class="col-sm-9 col-xs-12">

                            <label class='radio-inline'>
                                <input type='radio' name='isdispatcharea' value='0' {if $dispatch['is_dispatch_area']==0}checked{/if} /> 不配送区域
                            </label>
                            <label class='radio-inline'>
                                <input type='radio' name='isdispatcharea' value='1' {if $dispatch['is_dispatch_area']==1}checked{/if} /> 只配送区域
                            </label>

                            <span class='help-block'>不配送区域:不配送选择的区域,只配送未选择的区域<br>只配送区域:只配送选择的区域,不配送未选择的区域<br>只能选择一种类型</span>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-2 control-label"><span id="dispatcharea_name">{if empty($dispatch['is_dispatch_area'])}不{else/}只{/if}</span>配送区域</label>
                        <div class="col-sm-9 col-xs-12">

                            <div id="areas" class="form-control-static">{$dispatch_nodispatchareas}</div>
                            <a href="javascript:;" class="btn btn-default" onclick="selectAreas()">选择地区</a>
                            <input type="hidden" id='selectedareas' name="nodispatchareas" value="{$dispatch_nodispatchareas}" />
                            <input type="hidden" id='selectedareas_code' name="nodispatchareas_code" value="{$dispatch_nodispatchareas_code}" />

                        </div>
                    </div>

                    <div class="form-group-title"></div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label ">状态</label>
                        <div class="col-sm-9 col-xs-12">

                            <label class='radio-inline'>
                                <input type='radio' name='enabled' value=1' {if $dispatch['enabled']==1}checked{/if} /> 启用
                            </label>
                            <label class='radio-inline'>
                                <input type='radio' name='enabled' value=0' {if $dispatch['enabled']==0}checked{/if} /> 禁用
                            </label>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label "></label>
                        <div class="col-sm-9 col-xs-12">

                            <input type="submit" value="提交" class="btn btn-primary"  />

                            <input type="button" name="back" onclick='cl()' value="返回列表" class="btn btn-default" />
                        </div>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{include file="dispatch/select_areas" /}

<!-- 全局js -->

<script src="__PUBLIC__/admin/js/bootstrap.min.js?v=3.3.6"></script>
<!-- 自定义js -->
<script type="text/javascript" src="__PUBLIC__/admin/Huploadify/jquery.Huploadify.js"></script>
<script src="__PUBLIC__/admin/js/plugins/layer/layer.min.js"></script>
<script src="__PUBLIC__/admin/js/plugins/validate/jquery.validate.min.js"></script>
<!-- iCheck -->
<script src="__PUBLIC__/admin/js/plugins/iCheck/icheck.min.js"></script>
<script src="__PUBLIC__/admin/js/common/admin.js"></script>
<script src="__PUBLIC__/admin/js/common/ajax.js"></script>
<script language='javascript'>
    function show_type(flag){
        if (flag == 1) {
            $('.show_h').hide();
            $('.show_n').show();
        } else {
            $('.show_h').show();
            $('.show_n').hide();
        }
    }
    $(function(){
        show_type({$dispatch['calculate_type']});
        $(':radio[name=calculatetype]').click(function(){
            var val = $(this).val();
            show_type(val);
        })
        $(':radio[name=dispatchtype]').click(function(){
            var val = $(this).val();
            $(".dispatch0,.dispatch1").hide();
            $(".dispatch" + val).show();
        })

        $(':radio[name=isdispatcharea]').click(function(){
            var val = $(this).val();
            var name = '不';
            if(val == 1) {
                name = '只';
            }
            $("#dispatcharea_name").html(name);
        })

        $("select[name=express]").change(function(){
            var obj = $(this);
            var sel = obj.find("option:selected");
            $(":input[name=expressname]").val(sel.data("name"));
        });
        {if !empty($dispatch['express'])}
        $("select[name=express]").val("{$dispatch['express']}");
        {/if}


        var icon = "<i class='fa fa-times-circle'></i>&nbsp;&nbsp;";
        $('#form').validate({
            errorElement : 'span',
            debug: true,//只验证不提交表单
            //layer ajax提交表单
            submitHandler:function(){
                post(postUrl,'POST',$('#form').serialize(),1);
                return false;
            },//这是关键的语句，配置这个参数后表单不会自动提交，验证通过之后会去调用的方法

            rules:{
                dispatchname:{required:true},
                sort:{required:true},
            },

            messages:{
                dispatchname:{required:icon+'必填'},
                sort:{required:icon+'必填'},
            }
        });


    });

    function getCurrents(withOutRandom){
        var citys = "";
        $('.citys').each(function(){
            var crandom = $(this).prev().val();
            if (withOutRandom && crandom == withOutRandom){
                return true;
            }
            citys += $(this).val();
        });
        return citys;
    }
    function getCurrentsCode(withOutRandom){
        var citys = "";
        $('.citys_code').each(function(){
            var crandom = $(this).prev().prev().prev().val();
            if (withOutRandom && crandom == withOutRandom){
                return true;
            }
            citys += $(this).val();
        });
        return citys;
    }
    var current = '';
    function addArea(btn){
        $(btn).button('loading');
        $.ajax({
            url:"{:url('dispatch/tpl')}",
            dataType:'json',
            success:function(json){
                console.log(json)
                $(btn).button('reset');
                current = json.random;
                $('#tbody-areas').append(json.html);
                $('#tbody-areas tr').last().hide();
                clearSelects();
                $("#modal-areas").modal();

                var citystrs = "";

                var currents = getCurrentsCode();
                currents = currents.split(';');

                var parentdisabled = false;
                for (var i in currents){
                    var area_info = currents[i].split(' ');
                    if(area_info[0]) {
                        $('#ch_pcode'+area_info[0]).prop("disabled",true);
                    }
                    if(area_info[1]) {
                        $('#ch_ccode'+area_info[1]).prop("disabled",true);
                    }
                    if(area_info[2]) {
                        $('#ch_acode'+area_info[2]).prop("disabled",true);
                    }
                }

                $('#btnSubmitArea').unbind('click').click(function(){
                    update_area();
                    var city_html = $('#city_info').html();
                    var area_html = $('#area_info').html();

                    if (city_html && area_html) {
                        $('.' + current + ' .cityshtml').html(city_html);
                        $('.' + current + ' .citys').val(city_html);
                        $('.' + current + ' .citys_code').val(area_html);
                        $('#tbody-areas tr').last().show();
                    }

                })


                var calculatetype1 = $('input[name="calculatetype"]:checked ').val();
                show_type(calculatetype1);
            }
        })
    }

</script>
